package com.udp.nml.example;

import java.nio.ByteBuffer;

import org.apache.mina.core.buffer.IoBuffer;

public class Packet 
{
	private char checksum;
	private byte[] data = new byte[0];
	private byte modemDataType = 7;
	private byte modemReservedByte = 0;
	private byte packetId;
	private char serialNumber;
	private byte type;
	
	public Packet()
	{
		
	}
	
	public Packet(byte[] bytes)
	{
		assert bytes != null;
		assert bytes.length >= 10;
		
		ByteBuffer buffer = ByteBuffer.wrap(bytes);
		byte type = buffer.get();
		byte packetId = buffer.get();
		byte modemDataType = buffer.get();
		byte modemReservedType = buffer.get();
		char serialNumber = buffer.getChar();
		
		int length = bytes.length - 8;
		
		byte[] data = new byte[length];
		buffer.get(data, 0, length);
		
		char checksum = buffer.getChar();
		
		setType(type);
		setModemDataType(modemDataType);
		setChecksum(checksum);
		setPacketId(packetId);
		setSerialNumber(serialNumber);
		setModemReservedByte(modemReservedType);
		setData(data);
	}
	
	public Packet(IoBuffer buffer)
	{
		buffer = buffer.shrink();
		
		byte type = buffer.get();
		byte packetId = buffer.get();
		byte modemDataType = buffer.get();
		byte modemReservedType = buffer.get();
		char serialNumber = buffer.getChar();
		
		int length = buffer.array().length - 8;
		
		length = buffer.limit() - 8;
		
		byte[] data = new byte[length];
		// THIS IS DODGY WHEN CLIENT RECEIVES THE RESPONSE.
		//// SHRINKING BUFFER DOESN"T WPRL
		buffer.get(data, 0, length);
		
		String string = new String(data);
		
		char checksum = buffer.getChar();
		
		setType(type);
		setModemDataType(modemDataType);
		setChecksum(checksum);
		setPacketId(packetId);
		setSerialNumber(serialNumber);
		setModemReservedByte(modemReservedType);
		setData(data);
	}
	
	public byte[] getBytes()
	{		
		ByteBuffer byteBuffer = ByteBuffer.allocate(8 + data.length);
		byteBuffer.put(type);
		byteBuffer.put(packetId);
		byteBuffer.put(modemDataType);
		byteBuffer.put(modemReservedByte);
		byteBuffer.putChar(serialNumber);
		byteBuffer.put(data);
		byteBuffer.putChar(checksum);
		byte[] array = byteBuffer.array();
		return array;
	}
	
	public char getChecksum() {
		return checksum;
	}
	public void setChecksum(char checksum) {
		this.checksum = checksum;
	}
	public byte[] getData() {
		return data;
	}
	public void setData(byte[] data) {
		this.data = data;
	}
	public byte getModemDataType() {
		return modemDataType;
	}
	public void setModemDataType(byte modemDataType) {
		this.modemDataType = modemDataType;
	}
	public byte getModemReservedByte() {
		return modemReservedByte;
	}
	public void setModemReservedByte(byte modemReservedByte) {
		this.modemReservedByte = modemReservedByte;
	}
	public byte getPacketId() {
		return packetId;
	}
	public void setPacketId(byte packetId) {
		this.packetId = packetId;
	}
	public char getSerialNumber() {
		return serialNumber;
	}
	public void setSerialNumber(char serialNumber) {
		this.serialNumber = serialNumber;
	}
	public byte getType() {
		return type;
	}
	public void setType(byte type) {
		this.type = type;
	}
	
	@Override
	public String toString() 
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("checksum[");
		buffer.append(checksum);
		buffer.append("], data[");
		buffer.append(data);
		buffer.append("], modemDataType[");
		buffer.append(modemDataType);
		buffer.append("], modemReservedByte[");
		buffer.append(modemReservedByte);
		buffer.append("], packetId[");
		buffer.append(packetId);
		buffer.append("], serialNumber[");
		buffer.append(serialNumber);
		buffer.append("], type[");
		buffer.append(type);
		buffer.append("], dataAsString[" + new String(data));
		
		return buffer.toString();
	}
	
	@Override
	public boolean equals(Object obj) 
	{
		if (obj instanceof Packet)
		{
			Packet object = (Packet) obj;
			boolean equal = true;
			return true;
		}
		else
		{
			return super.equals(obj);
		}
	}
}
